﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PDFLibNet;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace kiosk
{
    // delegate used to call the main form function

    public delegate void updateDelToolStripPBar(int i);
    
    public partial class ShowTextData : Form
    {
        private Int32 DataID;
        private PDFLibNet.PDFWrapper mPdfDoc;
        private FileStream fs;
        private bool drag = false;
        private Point startPoint;
        private Int32 mHScrollValue;
        private Int32 mVcrollValue;
        private Double mcurrVcrollValue;
        private Double mcurrHcrollValue;
        private bool gotoNextP = false;
        private bool gotoPrevP = false;
        private DataBaseHandler handler;
        private Int32 pageNumber;
        private TaskbarHide.FormState fm;
        private Byte m_DataOptArray;
        private ManualResetEvent m_WinrsetEvent;
         
        //
        
        
        # region Scroll Constant
        
        private const int WM_SCROLL = 276; // Horizontal scroll
        private const int WM_VSCROLL = 277; // Vertical scroll
        private const int WM_HSCROLL = 276;
        private const int SB_LINEUP = 1; // Scrolls one line up
        private const int SB_LINELEFT = 1;// Scrolls one cell left
        private const int SB_LINEDOWN = 1; // Scrolls one line down
        private const int SB_LINERIGHT = 1;// Scrolls one cell right
        private const int SB_PAGEUP = 2; // Scrolls one page up
        private const int SB_PAGELEFT = 2;// Scrolls one page left
        private const int SB_PAGEDOWN = 3; // Scrolls one page down
        private const int SB_PAGERIGTH = 3; // Scrolls one page right
        private const int SB_PAGETOP = 6; // Scrolls to the upper left
        private const int SB_LEFT = 1; // Scrolls to the left
        private const int SB_PAGEBOTTOM = 7; // Scrolls to the upper right
        private const int SB_RIGHT = 1; // Scrolls to the right
        private const int SB_ENDSCROLL = 8; // Ends scroll
        private const int SB_BOTTOM = 0;
      
       


     #endregion

        #region Dll Api calls
        
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

       
     #endregion

         
        
        public ShowTextData(Int32 dataid)
        {
            InitializeComponent();
             
                       
            #region EventHandlers
            this.xtendPicBox1.PictBox.MouseDown += new System.Windows.Forms.MouseEventHandler(xtendPicBox1_MouseDown);
            this.xtendPicBox1.PictBox.MouseUp += new System.Windows.Forms.MouseEventHandler(xtendPicBox1_MouseUp);
            this.xtendPicBox1.PictBox.MouseMove += new System.Windows.Forms.MouseEventHandler(xtendPicBox1_MouseMove);
            this.prevPage.Click +=new EventHandler(prevPage_Click);
            this.nextBtn.Click +=new EventHandler(nextBtn_Click);
          
            
            //
            this.vScrollBar.Scroll += new ScrollEventHandler(vScrollBar_Scroll);
            this.hScrollBarEx.Scroll += new ScrollEventHandler(hScrollBarEx_Scroll);
            
           

            #endregion
            this.DataID = dataid;


            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
           
           
             
        }

        #region customScrollBar
        void hScrollBarEx_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.NewValue == this.mHScrollValue)
                e.NewValue -= 1;
            this.xtendPicBox1.HorizontalScroll.Value = e.NewValue;
             
            
        }

        void vScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.NewValue == this.mVcrollValue)
                e.NewValue -= 1;
            this.xtendPicBox1.VerticalScroll.Value = e.NewValue;
          
        }
        #endregion

       
        
        private void ShowTextData_Load(object sender, EventArgs e)
        {
            // get the dataid;
            try {
                fm = new TaskbarHide.FormState();
                fm.Maximize(this);
                // start the worker thread
                 
               
               
                //
                this.handler = new DataBaseHandler();
                this.handler.parpreConn();
                this.m_DataOptArray = this.handler.getDataOpt(this.DataID);
                label1.Text  = this.handler.getDataHeading(this.DataID);
                this.handler.getTxtData(this.DataID);
                this.pageNumber = 1;
                openPDF("temp.pdf");
                this.refreshPage();
                this.TopMost = true;
                

            }
            catch(Exception ex)
            {

                MessageBox.Show(ex.Message);
            
            }
            
           
        }
       
        private bool LoadFile(string filename, PDFLibNet.PDFWrapper pdfDoc)
        {
            try
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                //Does not supported by MuPDF.                
                //fs = new System.IO.FileStream(filename, System.IO.FileMode.Open);                
                //return pdfDoc.LoadPDF(fs);                
                bool bRet =  pdfDoc.LoadPDF(filename);               
               
                return bRet;                
            }
            catch (System.Security.SecurityException)
            {
                return false;
                
            }
        }

       

        #region mouse handler

        void xtendPicBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.drag)
            {
                 
                
                Point p1 = new Point(e.X, e.Y);
                this.mcurrHcrollValue = this.xtendPicBox1.HorizontalScroll.Value;
                this.mcurrVcrollValue = this.xtendPicBox1.VerticalScroll.Value;
                if (p1.X < this.startPoint.X) 
                {

                    this.mcurrHcrollValue -= .8;
                    if (this.mcurrHcrollValue > 0 )
                        this.xtendPicBox1.HorizontalScroll.Value = Convert.ToInt32(this.mcurrVcrollValue);

                    
                    // going to the left side
                
                }
                if (p1.X > this.startPoint.X )
                {

                    this.mcurrHcrollValue += 100;
                    if (this.mcurrHcrollValue < this.mVcrollValue)
                        SendMessage(this.xtendPicBox1.Handle, WM_HSCROLL, (System.IntPtr)SB_RIGHT, System.IntPtr.Zero);

                    
                   // going to the right
                
                }

                if (p1.Y > this.startPoint.Y)
                {
                    this.mcurrVcrollValue += 100;
                    if(this.mcurrVcrollValue < this.mHScrollValue)
                    //this.xtendPicBox1.VerticalScroll.Value = Convert.ToInt32(this.mcurrVcrollValue);
                        SendMessage(this.xtendPicBox1.Handle, WM_VSCROLL, (System.IntPtr)SB_LINEDOWN, System.IntPtr.Zero);
                    // GOING down SB_LINEUP
                
                }

                if (p1.Y < this.startPoint.Y )
                {

                    this.mcurrVcrollValue -= .8;
                    if (this.mcurrVcrollValue > 0)
                        this.xtendPicBox1.VerticalScroll.Value = Convert.ToInt32(this.mcurrVcrollValue);

                   
                           
                   // going up 
                }

              

            }
            
         
        }

        void xtendPicBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
           // MessageBox.Show("Mouse up");
            this.drag = false;

            
        }

        void xtendPicBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.drag = true;
            if(this.startPoint == null)
			{
			
            this.startPoint = new Point();
            }
			this.startPoint.X = e.X;
            this.startPoint.Y = e.Y;
            //MessageBox.Show(this.xtendPicBox1.AutoScrollMargin.Height.ToString() + "Height" + this.xtendPicBox1.AutoScrollMargin.Width.ToString());
            //MessageBox.Show("Mouse Down");

        }
        
        #endregion

        private void nextBtn_Click(object sender, EventArgs e)
        {
           if(this.pageNumber < this.mPdfDoc.PageCount)
           {
               // MessageBox.Show(mPdfDoc.PageCount.ToString());
                 
                this.pageNumber += 1;
                this.gotoNextP = true;
                refreshPage();
               
                this.TopMost = true;
                
                //MessageBox.Show(this.pageNumber.ToString() +"----"+this.mPdfDoc.CurrentPage.ToString());
            
           }
            else
            {
                this.gotoNextP = false;
                MessageBox.Show("There is no more next page !! Current Page No :" + pageNumber.ToString());
            }

        }

        private void prevPage_Click(object sender, EventArgs e)
        {
            if (this.pageNumber > 1)
            {
                
                
                this.pageNumber -= 1;
                this.gotoPrevP = true; 
                refreshPage();
                
                this.TopMost = true;

            }
            else
            {
                this.gotoPrevP = false;
                MessageBox.Show("There is no more Previous page !! Current Page No :" + pageNumber,ToString());
            }

        }

        private void refreshPage()
        
        {
           

            

            if ((m_DataOptArray & (Byte)RotateView.Default) == (Byte)RotateView.Default)
            {
                // The default view is set

                if (this.gotoNextP)
                {
                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else if(this.gotoPrevP)
                {
                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else 
                {
                    this.mPdfDoc.CurrentPage = 1;
                }
                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);



                this.mPdfDoc.RenderPage(this.xtendPicBox1.Handle);

                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;
            
                
                //
               
               
           
                this.xtendPicBox1.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateBy90) == (Byte)RotateView.RotateBy90)
            {

                // rotate by 90 set

                if (this.gotoNextP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else if (this.gotoPrevP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else
                {
                    this.mPdfDoc.CurrentPage = 1;
                }
                
                
                // this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);


                this.mPdfDoc.RenderPage(this.xtendPicBox1.Handle);     
                
                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;
             
                
                
               
                bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
                System.Console.WriteLine("rotate 90");
                this.xtendPicBox1.Image = bitmap;
            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateBy180) == (Byte)RotateView.RotateBy180)
            {
                // rotate by 180 set

                if (this.gotoNextP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;


                }
                else if (this.gotoPrevP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else
                {
                    this.mPdfDoc.CurrentPage = 1;
                }
                
                
                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);

                this.mPdfDoc.RenderPage(this.xtendPicBox1.Handle);



                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;
          
           
                bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
                this.xtendPicBox1.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate 180");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateBy270) == (Byte)RotateView.RotateBy270)
            {
                // rotate by 270 is set
               // this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];  

                if (this.gotoNextP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else if (this.gotoPrevP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;

                }
                else
                {
                    this.mPdfDoc.CurrentPage = 1;
                }

                this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);

                this.mPdfDoc.RenderPage(this.xtendPicBox1.Handle);

                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;
           
                bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                this.xtendPicBox1.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate 270");


            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateByFlipx) == (Byte)RotateView.RotateByFlipx)
            {
                // rotate by flip x is selected
               // this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                if (this.gotoNextP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else if (this.gotoPrevP)
                {
                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else
                {
                    this.mPdfDoc.CurrentPage = 1;
                }

                this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);

                this.mPdfDoc.RenderPage(this.xtendPicBox1.Handle);


                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;
            
                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
                this.xtendPicBox1.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate x");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateByFlipy) == (Byte)RotateView.RotateByFlipy)
            {
                // rota by y is selected 

                if (this.gotoNextP)
                {
                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else if (this.gotoPrevP)
                {
                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else
                {
                    this.mPdfDoc.CurrentPage = 1;
                }
                
                
                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];

                this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);

                this.mPdfDoc.RenderPage(this.xtendPicBox1.Handle);


                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;
            

           
                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                this.xtendPicBox1.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate y");

            }

            else if ((m_DataOptArray & (Byte)RotateView.RotateFlipxy) == (Byte)RotateView.RotateFlipxy)
            {
                // Rotate by xy is selected

                //this.mPdfDoc.CurrentPage = this.pageNumber;//[this.pageNumber.Count - 1];
                
                if (this.gotoNextP)
                {

                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else if (this.gotoPrevP)
                {
                    this.mPdfDoc.CurrentPage = this.pageNumber;
                }
                else
                {
                    this.mPdfDoc.CurrentPage = 1;
                }
                this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);

                this.mPdfDoc.RenderPage(this.xtendPicBox1.Handle);


                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                Bitmap bitmap = _backbuffer;
            
                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipXY);
                this.xtendPicBox1.Image = bitmap;
                System.Console.WriteLine("Dedfault rotate xy");


            }

            //this.mPdfDoc.FitToWidth(this.xtendPicBox1.Handle);
            //
            this.mHScrollValue = this.mPdfDoc.PageHeight;
            this.mVcrollValue = this.mPdfDoc.PageWidth;
            //this.xtendPicBox1.VerticalScroll.Maximum = this.mVcrollValue;
            //this.xtendPicBox1.HorizontalScroll.Maximum = this.mHScrollValue;
            this.vScrollBar.Maximum = this.xtendPicBox1.VerticalScroll.Maximum;
            this.hScrollBarEx.Maximum = this.xtendPicBox1.HorizontalScroll.Maximum;
            //this.m_WinrsetEvent.Set();
            this.xtendPicBox1.Invalidate();
            //this.xtendPicBox1.PictBox.Invalidate();
            
             
        }


       // 

        private void updateCofig()
        {

            try {
               
                this.m_DataOptArray = this.handler.getDataOpt(this.DataID);
                this.label1.Text = handler.getDataHeading(this.DataID);
                         
            }
            catch(Exception ex)
            {
                MessageBox.Show("There was an error" +ex.Message);
            }


        
        }


        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.O))
            {
                this.TopMost = false;
                DataDisplayOption disopt = new DataDisplayOption(this.DataID);
                disopt.ShowDialog();
                this.updateCofig();
                refreshPage();
                this.TopMost = true;
            }

            if (keyData == Keys.F1)
            {
                this.TopMost = false;
                AboutBox1 ab = new AboutBox1();
                ab.ShowDialog();
                this.TopMost = true;

            }

            
            if (keyData == (Keys.Control | Keys.H))
            {
                this.fm.Restore(this);

            }


            return base.ProcessCmdKey(ref msg, keyData);

        }



        private void openPDF(string path)
        {
            this.mPdfDoc = new PDFLibNet.PDFWrapper();
            this.mPdfDoc.PDFLoadBegin += new PDFLoadBeginHandler(mPdfDoc_PDFLoadBegin);
            this.mPdfDoc.PDFLoadCompeted += new PDFLoadCompletedHandler(mPdfDoc_PDFLoadCompeted);
            this.mPdfDoc.LoadPDF(path);
           

        }


        private void resetWindow()
        {
            this.TopMost = true;           

        }
        
        
        
        private void exitBtn_Click(object sender, EventArgs e)
        {
            
            this.Close();

        }


        #region busyWinWork
      
        
        
        private void Start()
        {
            if (showBusyWindow.IsBusy)
            {
                Stop();
            }
            showBusyWindow.RunWorkerAsync();
            
        }

        private void Stop()
        {
            if (showBusyWindow.IsBusy)
            {
                showBusyWindow.CancelAsync();
                
            }
        }



        private void showBusyWindow_DoWork(object sender, DoWorkEventArgs e)
        {
            
        }


        #endregion

        void mPdfDoc_PDFLoadCompeted()
        {
            
             
        }

        void mPdfDoc_PDFLoadBegin()
        {

            
            //throw new NotImplementedException();
        }

      


    } // End of the class

  

}

